<template>
    <div>
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>文件导出</span>
                </div>
            </template>
            <div>
                <div>
                    <el-button type="primary" @click="exportFile">导出</el-button>
                </div>
                <el-table :data="list" :height="propHeight">
                    <el-table-column type="index" label="序号" width="120px" />
                    <el-table-column prop="id" label="id"></el-table-column>
                    <el-table-column prop="name" label="姓名"></el-table-column>
                    <el-table-column prop="date" label="日期"></el-table-column>
                    <el-table-column prop="address" label="住址"></el-table-column>
                </el-table>
            </div>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { computed, reactive, ref, shallowRef } from 'vue';
import { _excelList } from "@/api/other";
import dayjs from "dayjs";
//引入插件
import * as xlsx from 'xlsx'

// 表格渲染
let list = shallowRef([])
getData()
async function getData() {
    let res = await _excelList()
    console.log(32, res);
    // 处理时间
    res.data.data.forEach((item: any) => {
        let newTime = dayjs(item.date).format('YYYY-MM-DD') // '25/01/2019'
        item.date = newTime
    });
    list.value = res.data.data
}


// 表格动态高度
let propHeight = ref("")
propHeightFn()
function propHeightFn() {
    let allHeight = document.body.clientHeight
    propHeight.value = allHeight - 220 + "px"
}
window.onresize = function () {
    propHeightFn()
}

let exportFile = () => {
    //创建一个 工作薄
    const workbook = xlsx.utils.book_new()

    //处理数据
    const tableCh = list.value?.map((v: any) => {
        return {
            id: v.id,
            名称: v.name,
            地址: v.address,
            时间: v.date,
            测试: 123
        }
    })
    console.log(73, tableCh);

    //创建一个工作表
    const worksheet = xlsx.utils.json_to_sheet(tableCh!)

    //工作簿导入工作表
    xlsx.utils.book_append_sheet(workbook, worksheet)

    //导出
    xlsx.writeFile(workbook, 'abc.xlsx')
}


</script>

<style scoped></style>